package com.zdb.order.controller;

import com.fasterxml.jackson.annotation.JsonFormat;

import com.zdb.order.common.PageResult;
import com.zdb.order.entity.*;
import com.zdb.order.pojo.ListQuery;
import com.zdb.order.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("user")
public class UserController {
    @Autowired
    private UserService userService;
    @Autowired
    private ProvincesService provincesService;
    @Autowired
    private CitysService citysService;
    @Autowired
    private DistrictService districtService;
    @Autowired
    private AddressService addressService;

    @GetMapping("tt")
    public ResponseEntity<Void> tt(String tt){
        System.out.println(tt);
        return ResponseEntity.ok(null);
    }

    @PostMapping("page")
//    public ResponseEntity<PageResult<Void>> queryUserPage(
    public ResponseEntity<PageResult<User>> queryUserPage(
              @RequestBody ListQuery listQuery
            ){
        System.out.println("tt");
        if(!StringUtils.isEmpty(listQuery.getDate())){
            System.out.println("date:"+listQuery.getDate().toString());
        }
        PageResult<User>  result = userService.queryUserPage(listQuery.getPage()+"",listQuery.getLimit()+"",listQuery.getSort(),listQuery.getName(),listQuery.getQq(),listQuery.getTel(),listQuery.getDate());
        if (result == null ||result.getItems().size() == 0) {
            return new ResponseEntity<>(HttpStatus.NOT_FOUND);
        }
        System.out.println(result.toString());
//        System.out.println("id"+result.getItems().get(0).getId());

        //为User对象，查询省市区名称并且拼接，和查询address表中的详细地址进行拼接
        StringBuffer sb = new StringBuffer();
        for(int i = 0;i<result.getItems().size();i++){
            Provinces provinces = provincesService.queryProvinces(result.getItems().get(i).getPid());
//            System.out.println("provinces"+provinces.toString());
            if(!StringUtils.isEmpty(provinces)){
                sb.append(provinces.getName());
            }
            Citys citys = citysService.queryCitys(result.getItems().get(i).getCid());
            if(!StringUtils.isEmpty(citys)){
                sb.append(citys.getName());
            }
            District district = districtService.queryDistrict(result.getItems().get(i).getDid());
            if(!StringUtils.isEmpty(district)){
                sb.append(district.getName());
            }

            result.getItems().get(i).setPcd(sb.toString());
            sb.delete(0,sb.length());

            System.out.println("pcd"+result.getItems().get(i).getPcd());
            //---------------------------------------------------------
            //传入uid，查询address表中详细地址
            List<Address>  resultAddressLists = addressService.selectAddressLisrts(result.getItems().get(i).getId());
            if(!StringUtils.isEmpty(resultAddressLists)){
                result.getItems().get(i).setListAddress(resultAddressLists);
                System.out.println("resultAddressLists"+resultAddressLists);
            }
        }

        return ResponseEntity.ok(result);
//        return ResponseEntity.ok(null);
    }
    @PostMapping("editRowUser")
    public ResponseEntity<Void> editRowUser(@RequestBody User editObject){
        int resultCount = userService.editRowUser(editObject);
        if(resultCount!=0){
            return new ResponseEntity<>(HttpStatus.OK);
        }
        return ResponseEntity.ok(null);
    }

    @PostMapping("deleteRowUser")
    public ResponseEntity<Void> deleteRowUser(@RequestBody User deleteObject){
        int resultCountUser = userService.deleteRowUser(deleteObject);
        int resultCountAddress = addressService.deleteAddressByUserId(deleteObject.getId());
        if(resultCountUser!=0){
            return new ResponseEntity<>(HttpStatus.OK);
        }
        return ResponseEntity.ok(null);
    }
}
